package de.eridea.learning.java.usermanagement.persistence.springdao;

import javax.persistence.EntityManager;

import org.springframework.orm.jpa.JpaTemplate;
import org.springframework.orm.jpa.support.JpaDaoSupport;

import de.eridea.learning.java.usermanagement.persistence.Group;
import de.eridea.learning.java.usermanagement.persistence.User;
import de.eridea.learning.java.usermanagement.persistence.UserManager;
import de.eridea.learning.java.usermanagement.persistence.UserManagerImpl;
import de.eridea.learning.java.usermanagement.persistence.VerificationFailedException;

public class UserManagerJpaDaoSupportWrapper extends JpaDaoSupport implements
		UserManager {

	private UserManagerImpl userManager;

	public User getUser(String loginName) {
		return userManager.getUser(loginName);
	}

	public User login(String loginName, char[] password)
			throws VerificationFailedException {
		return userManager.login(loginName, password);
	}

	public User createUser(String loginName, char[] password) {
		return userManager.createUser(loginName, password);
	}

	public void createGroup(String groupName, User owner) {
		userManager.createGroup(groupName, owner);
	}

	public void addUserToGroup(User user, Group group) {
		userManager.addUserToGroup(user, group);
	}

	public void removeUserFromGroup(User user, Group group) {
		userManager.removeUserFromGroup(user, group);
	}

	protected void initDao() throws Exception {
		if (true) throw new Exception("just remove the line throwing this exception");
		JpaTemplate jpaTemplate = getJpaTemplate();
		EntityManager entityManager = jpaTemplate.getEntityManager();
		userManager.setEntityManager(entityManager);
	}

	public UserManagerImpl getUserManager() {
		return userManager;
	}

	public void setUserManager(UserManagerImpl userManager) {
		this.userManager = userManager;
	}
}
